class Operation < ActiveRecord::Base
  belongs_to :video

  before_destroy :revert

  def Operation.start
    fork do
      Operation.work
    end
    return true
  end

  def Operation.work
    if Operation.count == 0
      return
    end

    op = Operation.find :first
    if !op.nil?
      if op.locked
        return false
      end
      op.locked = true
      op.save

      if !op.video.nil?
        if op.operation == "transcode"
          op.video.transcode
        elsif op.operation == "download"
          op.video.download
        end
      end
    end
    op.destroy
    Operation.work
  end

  def revert
    self.video.status = 'none'
  end
end
